import domains from "../config/domains";
const DEFAULT_DOMAIN_KEY = "zg99";
export function request(config) {
  const {
    url,
    method = "GET",
    data = {},
    header = {},
    domainKey = DEFAULT_DOMAIN_KEY,
  } = config;

  const baseURL = domains[domainKey] || domains.zg99;
  const fullUrl = `${baseURL}${url}`;
  return new Promise((resolve, reject) => {
    xhs.request({
      url: fullUrl,
      method,
      data,
      header,
      success: (res) => {
        if (res.statusCode >= 200 && res.statusCode < 300) {
          resolve(res.data);
        } else {
          // xhs.showToast({ title: "请求失败", icon: "none" });
          reject(res);
        }
      },
      fail: (err) => {
        xhs.showToast({ title: "网络异常，请重试", icon: "none" });
        reject(err);
      },
    });
  });
}

export function uploadFile(config) {
  const {
    url,
    method = "GET",
    filePath = {},
    header = {},
    domainKey = DEFAULT_DOMAIN_KEY,
    name
  } = config;

  const baseURL = domains[domainKey] || domains.zg99;
  const fullUrl = `${baseURL}${url}`;
  return new Promise((resolve, reject) => {
    xhs.uploadFile({
      url: fullUrl,
      method,
      filePath,
      header,
      name,
      success: (res) => {
        if (res.statusCode >= 200 && res.statusCode < 300) {
          resolve(res.data);
        } else {
          // xhs.showToast({ title: "请求失败", icon: "none" });
          reject(res);
        }
      },
      fail: (err) => {
        xhs.showToast({ title: "网络异常，请重试", icon: "none" });
        reject(err);
      },
    });
  });
}